﻿using Mapster;
using Microsoft.AspNetCore.Mvc;
using WYD.Common;
using WYD.Infrastructure.Attribute;
using WYD.Infrastructure.Controllers;
using WYD.Infrastructure.Enums;
using WYD.Infrastructure.Model;
using WYD.Infrastructure.WebExtensions;
using WYD.Model;
using WYD.ServiceCore.Filters;
using WYD.ServiceCore.Model;
using WYD.ServiceCore.Model.Dto;
using WYD.ServiceCore.Services.IService;

namespace WYD.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 数据字典信息
    /// </summary>
    [Route("system/dict/type")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class SysDictTypeController : BaseController
    {
        private readonly ISysDictService _sysDictService;

        public SysDictTypeController(ISysDictService sysDictService)
        {
            _sysDictService = sysDictService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="pagerInfo"></param>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "system:dict:list")]
        [HttpGet("list")]
        public IActionResult List([FromQuery] SysDictType dict, [FromQuery] PagerInfo pagerInfo)
        {
            var list = _sysDictService.SelectDictTypeList(dict, pagerInfo);

            return SUCCESS(list, TIME_FORMAT_FULL);
        }

        /// <summary>
        /// 查询全部
        /// </summary>
        /// <param name="dict"></param>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "system:dict:list")]
        [HttpGet("listAll")]
        public IActionResult ListAll([FromQuery] SysDictType dict)
        {
            var list = _sysDictService.Queryable()
                .WhereIF(!string.IsNullOrEmpty(dict.DictType), it => it.DictType.Contains(dict.DictType))
                .ToList();
            return SUCCESS(list, TIME_FORMAT_FULL);
        }

        /// <summary>
        /// 查询字典类型详细
        /// </summary>
        /// <param name="dictId"></param>
        /// <returns></returns>
        [HttpGet("{dictId}")]
        [ActionPermissionFilter(Permission = "system:dict:query")]
        public IActionResult GetInfo(long dictId = 0)
        {
            return SUCCESS(_sysDictService.GetInfo(dictId));
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "system:dict:add")]
        [Log(Title = "字典操作", BusinessType = BusinessType.INSERT)]
        [HttpPost("edit")]
        public IActionResult Add([FromBody] SysDictTypeDto dto)
        {
            SysDictType dict = dto.Adapt<SysDictType>();
            if (UserConstants.NOT_UNIQUE.Equals(_sysDictService.CheckDictTypeUnique(dict)))
            {
                return ToResponse(ApiResult.Error($"新增字典'{dict.DictName}'失败，字典类型已存在"));
            }
            dict.CreateBy = HttpContext.GetUId();
            dict.CreateName = HttpContext.GetNickName();
            dict.CreateTime = DateTime.Now;
            return SUCCESS(_sysDictService.InsertDictType(dict));
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "system:dict:edit")]
        [Log(Title = "字典操作", BusinessType = BusinessType.UPDATE)]
        [Route("edit")]
        [HttpPut]
        public IActionResult Edit([FromBody] SysDictTypeDto dto)
        {
            SysDictType dict = dto.Adapt<SysDictType>();
            if (UserConstants.NOT_UNIQUE.Equals(_sysDictService.CheckDictTypeUnique(dict)))
            {
                return ToResponse(ApiResult.Error($"修改字典'{dict.DictName}'失败，字典类型已存在"));
            }
            //设置添加人
            dict.UpdateBy = HttpContext.GetUId();
            dict.UpdateName = HttpContext.GetNickName();
            return SUCCESS(_sysDictService.UpdateDictType(dict));
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <returns></returns>
        [ActionPermissionFilter(Permission = "system:dict:remove")]
        [Log(Title = "删除字典类型", BusinessType = BusinessType.DELETE)]
        [HttpDelete("{ids}")]
        public IActionResult Remove(string ids)
        {
            long[] idss = Tools.SpitLongArrary(ids);

            return SUCCESS(_sysDictService.DeleteDictTypeByIds(idss));
        }

        /// <summary>
        /// 修改字典类型状态
        /// </summary>
        /// <param name="dictType">字典类型对象</param>
        /// <returns></returns>
        [HttpPut("changeStatus")]
        [Log(Title = "修改字典类型状态", BusinessType = BusinessType.UPDATE)]
        [ActionPermissionFilter(Permission = "system:dict:edit")]
        public IActionResult ChangeStatus([FromBody] SysDictType dictType)
        {
            var result = _sysDictService.UpdateDictTypeStatus(dictType);

            return ToResponse(result);
        }
        
        /// <summary>
        /// 字典导出
        /// </summary>
        /// <returns></returns>
        [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "字典导出")]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "system:dict:export")]
        public IActionResult Export()
        {
            var list = _sysDictService.GetAll();

            string sFileName = ExportExcel(list, "sysdictType", "字典");
            return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
        }
    }
}
